package com.foreveross.chameleon.utils;

import com.foreveross.chameleon.config.ChameleonConstants;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

/**
 * @description preferences util for chameleon
 * @author zhouzhineng
 *
 */
public class PreferencesUtil {

	protected Context mContext;
	protected SharedPreferences mPreferences;
	private static PreferencesUtil mInstance;
	
	public static PreferencesUtil getInstance(Context ctx) {
		if( mInstance == null) {
			mInstance = new PreferencesUtil(ctx);
		}
		
		return mInstance;
	}
	
	private PreferencesUtil(Context ctx) {
		mContext = ctx;
		mPreferences = PreferenceManager.getDefaultSharedPreferences(ctx);
	}
	
	public void saveToken(String token) {
		 Editor editor = mPreferences.edit();
	     editor.putString(ChameleonConstants.APP_TOKEN, token);
	     editor.commit();
	}
	
	public String getToken() {
		return mPreferences.getString(ChameleonConstants.APP_TOKEN, "");
	}
	
	public void saveTokenExpire(String expire) {
		Editor editor = mPreferences.edit();
	    editor.putString(ChameleonConstants.APP_TOKEN_EXPIRE, expire);
	    editor.commit();
	}
	
	public String getTokenExpire() {
		return mPreferences.getString(ChameleonConstants.APP_TOKEN_EXPIRE, "");
	}
	
	public Boolean isAppFirstStart() {
		Boolean isFirstStart =  mPreferences.getBoolean(ChameleonConstants.APP_FIRST_START, true);
		if(isFirstStart) {
			Editor editor = mPreferences.edit();
		    editor.putBoolean(ChameleonConstants.APP_FIRST_START, false);
		    editor.commit();
		}
		return isFirstStart;
	}
	
}
